package com.suny.fork_join;

import java.util.concurrent.*;

/**
 * fork join测试
 */
public class ForkJoinDemo {

    public static void main(String... args) throws ExecutionException, InterruptedException, TimeoutException {
        recursiveTask();
//        recursiveAction();
    }

    /**
     * 处理带返回值的fork join
     *
     * @author sunny
     * @date 2019/10/30 下午3:16
     */
    private static void recursiveTask() throws InterruptedException, ExecutionException, TimeoutException {
        ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();

        int[] array = {100, 400, 200, 90, 80, 300, 600, 10, 20, -10, 30, 2000, 1000};
        CustomRecursiveTask task = new CustomRecursiveTask(array, 0, array.length - 1);

        Future<Integer> future = forkJoinPool.submit(task);

        System.out.println("Result:" + future.get(1, TimeUnit.SECONDS));
    }

    /**
     * 无返回值的fork join
     *
     * @author sunny
     * @date 2019/10/30 下午3:18
     * @return void
     */
    private static void recursiveAction() throws ExecutionException, InterruptedException {
        ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();

        String s = "test recursive action";

        CustomRecursiveAction action = new CustomRecursiveAction(s);
        ForkJoinTask<Void> task = forkJoinPool.submit(action);
        System.out.println(task.get());
    }

}